package com.yh.test;


import com.sun.org.apache.xpath.internal.res.XPATHErrorResources_ja;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.net.URI;

public class hdfs_Test1 {
    static Configuration conf =new Configuration();
    static FileSystem fs;

    public static void main(String[] args) throws Exception {

        xiazai();
//        update();
//        create();
//        chakan();
        Getlist();

    }

    /**
     * 查看文件
     * @throws Exception
     */
    public static void Getlist()throws Exception{
        fs = FileSystem.get(conf);
        Path path = new Path("/");
        FileStatus[] list = fs.listStatus(path);

        for (FileStatus f : list){
            System.out.println(f);
        }
    }

    /**
     * 下载
     */
    public static void xiazai()throws Exception{
        fs = FileSystem.get(conf);
//        fs.copyToLocalFile(false,new Path("/yh-01.txt"),new Path("E:\\OneDrive\\桌面"),true);
        fs.copyToLocalFile(false,new Path("/yh-update.txt"),new Path("E:\\OneDrive\\桌面"),true); //查看是否追加文件成功
        fs.close();
    }

    /**
     * 修改hdfs文件名
     */
    public static void update()throws Exception{
        fs = FileSystem.get(new URI("hdfs://hadoop1:9000"),conf,"root");
        Path oldName = new Path("/yh.txt");
        Path newName = new Path("/yh-update.txt");
        fs.rename(oldName,newName);
    }

    /**
     * 创建目录和文件
     */
    public static void create()throws Exception{
        fs = FileSystem.get(new URI("hdfs://hadoop1:9000"),conf,"root");
        Path path = new Path("/yh-create");
        fs.mkdirs(path);
    }

    /**
     * 追加内容到文件
     */
    public static void chakan()throws Exception{
        fs = FileSystem.get(new URI("hdfs://hadoop1:9000"),conf,"root");
        String countName = "Hello HDFS";
        Path path = new Path("/yh-update.txt");
        byte[] bytes = countName.getBytes();
        FSDataOutputStream put = fs.append(path);
        put.write(bytes);

    }



}

